package com.witframework.dynoengine;

import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

import com.witframework.util.DateUtil;
import com.witframework.util.TimestampConverter;

public class Converter {
	public static String convertToString(Object cellValue) {
		if(cellValue==null){
			return "";
		}else if(cellValue instanceof Integer||cellValue.getClass()==int.class
				||cellValue instanceof Double||cellValue.getClass()==double.class
				||cellValue instanceof Float||cellValue.getClass()==float.class
				||cellValue instanceof Short||cellValue.getClass()==short.class){
			return cellValue.toString();
		}else if(cellValue instanceof Date){
			return DateUtil.convertDateToString((Date)cellValue);
		}else if(cellValue instanceof Timestamp){
			return DateUtil.getDateTime("yyyy-MM-dd hh:mm:ss", (Date)cellValue);
		}else if(cellValue instanceof Time){
			return DateUtil.getDateTime("hh:mm:ss", (Date)cellValue);
		}else if(cellValue instanceof java.lang.Byte[]||cellValue.getClass()==byte[].class){
			return new String((byte[])cellValue);
		}else if(cellValue instanceof Blob){
			return "...(binary content)";
		}else if(cellValue instanceof Clob){
			try {
				return ((Clob)cellValue).getSubString(1l, (int)((Clob)cellValue).length());
			} catch (SQLException e) {
				return "...(read data error)";
			}
		}else{
			return cellValue.toString();
		}
	}
	public static Object convertToObject(Class fieldType, String propertyValue) {
		try {
			if (fieldType == String.class) {
				return propertyValue;
			} else if (fieldType == Double.class
					|| fieldType == double.class) {
				return Double.valueOf(propertyValue);
			} else if (fieldType == Integer.class
					|| fieldType == int.class) {
				return Integer.valueOf(propertyValue);
			} else if (fieldType == Float.class
					|| fieldType == float.class) {
				return Float.valueOf(propertyValue);
			} else if (fieldType == Long.class
					|| fieldType == long.class) {
				return Long.valueOf(propertyValue);
			} else if (fieldType == java.util.Date.class) {
				return DateUtil.convertStringToDate(propertyValue);
			} else if (fieldType == java.sql.Timestamp.class) {
				return new TimestampConverter().convert(Date.class, propertyValue);
			} else if(fieldType == Byte.class
					|| fieldType == byte.class){
				return Byte.valueOf(propertyValue);
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}	
	
	public static Object convertToObject(int columnType, String propertyValue) {		
		try {
			if(columnType==Types.VARCHAR
					||columnType==Types.NVARCHAR
					||columnType==Types.CHAR
					||columnType==Types.LONGNVARCHAR){
				return propertyValue;				
			}else if(columnType==Types.BIGINT
					||columnType==Types.SMALLINT
					||columnType==Types.TINYINT
					||columnType==Types.INTEGER){
				return Integer.valueOf(propertyValue);	
			}else if(columnType==Types.DECIMAL
					||columnType==Types.DOUBLE
					||columnType==Types.NUMERIC
					||columnType==Types.FLOAT){				
				return  Double.valueOf(propertyValue);	
			}else if(columnType==Types.TIMESTAMP||columnType==Types.DATE){
				try{
					return DateUtil.convertStringToDate(propertyValue,"yyyy-MM-dd HH:mm:ss.S");					
				}catch(Exception ex1){
					try{
						return DateUtil.convertStringToDate(propertyValue,"yyyy-MM-dd HH:mm:ss");
					}catch(Exception ex2){						
					}
					try{
						return DateUtil.convertStringToDate(propertyValue,"yyyy-MM-dd HH:mm");
					}catch(Exception ex3){						
					}
					try{
						return DateUtil.convertStringToDate(propertyValue);
					}catch(Exception ex3){						
					}
				}				
			}else if(columnType==Types.TIME){
				try{
					return DateUtil.convertStringToDate(propertyValue,"HH:mm");
				}catch(Exception ex2){
						return DateUtil.convertStringToDate(propertyValue,"HH:mm:ss");
				}
			}			
		} catch (Exception e) {
			return null;
		}
		return propertyValue;
	}	
	
}
